import { createWebHashHistory, createRouter, RouteRecordRaw } from "vue-router";
import { getToken } from "@/utils/public";

const routes: RouteRecordRaw[] = [
	{
		path: "/",
		component: () => import("@/layout/index.vue"),
		redirect: "/menberList",
		children: [
			{
				path: "/menberList",
				name: "menberList",
				meta: { title: "人员管理" },
				component: () => import("@/view/user_config/index.vue"),
			},
			{
				path: "/roleList",
				name: "roleList",
				meta: { title: "角色管理" },
				component: () => import("@/view/role/index.vue"),
			},
			{
				path: "/dataSet",
				name: "dataSet",
				meta: { title: "数据权限" },
				component: () => import("@/view/data_permission/index.vue"),
			},
			{
				path: "/materialList",
				name: "materialList",
				meta: { title: "材料管理" },
				component: () => import("@/view/materials/index.vue"),
			},
			{
				path: "/orderList",
				name: "orderList",
				meta: { title: "订单管理" },
				component: () => import("@/view/order/index.vue"),
			},
			{
				path: "/question",
				name: "question",
				meta: { title: "工艺管理" },
				component: () => import("@/view/question/index.vue"),
			},

			// {
			// 	path: "offer",
			// 	name: "offer",
			// 	component: () => import("@/view/node/index.vue"),
			// },
		],
	},
	{
		path: "/login",
		component: () => import("@/view/login/index.vue"),
	},
	{
		path: "/excel",
		component: () => import("@/view/excel/index.vue"),
	},
	{
		// 404 路由
		path: "/:catchAll(.*)",
		meta: { title: "404" },
		component: () => import("@/components/404.vue"),
	},
];

const router = createRouter({
	history: createWebHashHistory(),
	routes,
});

// 白名单列表
const whitelist: string[] = ['/excel'];

let first = true; // 首次进入
router.beforeEach(async (to, from, next) => {
	console.log("router", to, from);

	// 是否是白名单
	if (whitelist.includes(to.path)) {
		next();
		return;
	}

	if (first) {
		// 获取token 登陆
		let token = getToken();
		if (!token) {
			next("/login");
		}
		first = false;
	}

	next();
});

export { router };
